"""
数据采集工具
用于从页面API获取数据并进行格式化处理
"""
import json
import requests
from typing import Dict, Any

class DataCollector:
    """数据采集器类"""
    
    def __init__(self, base_url: str = "https://test-tds-standard.cepin.com"):
        self.base_url = base_url
        self.session = requests.Session()
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
        }
    
    def login(self, username: str, password: str) -> bool:
        """登录系统"""
        login_url = f"{self.base_url}/api/auth/login"
        data = {
            "username": username,
            "password": password
        }
        try:
            response = self.session.post(login_url, json=data, headers=self.headers)
            if response.status_code == 200:
                return True
            return False
        except Exception as e:
            print(f"登录失败: {str(e)}")
            return False
    
    def get_organization_data(self) -> Dict[str, Any]:
        """获取组织画像数据"""
        # 从页面直接提取的数据
        org_data = {
            "total_employees": 30,
            "formal_employees": 27,
            "gender_ratio": "1:1.14",
            "avg_age": 42.6,
            "avg_tenure": "2年5月",
            "management_positions": 9,
            "reporting_depth": 2.00,
            "management_span": 3.63,
            "probation_count": 6,
            "party_member_ratio": 33.33,
            "campus_recruit_ratio": 46.67,
            "new_employee_ratio": 0.00,
            "turnover_rate": 0.00,
            "officer_soldier_ratio": "1:1.56"
        }
        
        # 人效分析数据
        performance_data = {
            "per_capita_profit": 0.92,  # 人均利润（万）
            "per_capita_gross_profit": 0.53,  # 人均毛利（万）
            "per_capita_income": 2.18  # 人均收入（万）
        }
        
        # 组织预警数据
        warning_data = {
            "age_warning": "干部男满59周岁、女满54周岁",
            "tenure_warning": True,
            "education_warning": "组织本科以下占比超过10%",
            "turnover_warning": True,
            "vacancy_warning": True
        }
        
        # 合并所有数据
        return {
            "organization": org_data,
            "performance": performance_data,
            "warnings": warning_data
        }
    
    def get_employee_list(self) -> list:
        """获取员工列表数据"""
        employees = [
            {
                "name": "吴盛厚",
                "department": "倍智集团",
                "position": "副总经理",
                "title": "副总经理",
                "level": "M3",
                "tags": ["久"]
            },
            {
                "name": "陈兰枫",
                "department": "倍智集团",
                "position": "总经理",
                "title": "总经理",
                "level": "M4",
                "tags": ["久"]
            },
            {
                "name": "吴悦珠",
                "department": "广州总部",
                "position": "分部副总经理",
                "title": "分部副总经理",
                "level": "M3",
                "tags": ["久"]
            },
            {
                "name": "喻寒妍",
                "department": "广州总部",
                "position": "分部总经理",
                "title": "分部总经理",
                "level": "M3",
                "tags": ["久"]
            },
            {
                "name": "周婕梅",
                "department": "信息技术部",
                "position": "架构师",
                "title": "架构师",
                "level": "P1",
                "tags": ["久", "留"]
            }
        ]
        return employees

    def format_data_for_comparison(self) -> Dict[str, Any]:
        """格式化数据用于对比"""
        org_data = self.get_organization_data()
        employee_data = self.get_employee_list()
        
        return {
            "organization_data": org_data,
            "employee_data": employee_data
        }
